import time
from selenium.webdriver.common.by import By
from common.base import Base
from common.functions import open_browser
from page.login_page import LoginPage
from page.shopcart_page import ShopCartPage
from page.index_page import IndexPage


class Order(Base):
    # 配送方式
    def distribution_method1(self):
        # 申通快递
        self.base_click((By.XPATH, '//input[@name="shipping" and @value="5"]'))
        time.sleep(2)

    def distribution_method2(self):
        # 邮局平邮
        self.base_click((By.XPATH, '//input[@name="shipping" and @value="6"]'))

    # 支付方式
    def pay1(self):
        # 余额支付
        self.base_click((By.XPATH, '//input[@name="payment" and @value="1"]'))
        time.sleep(2)

    def pay2(self):
        # 银行汇款/转账
        self.base_click((By.XPATH, '//input[@name="payment" and @value="2"]'))

    # 商品包装
    def package1(self):
        # 不要包装
        self.base_click((By.XPATH, '//input[@name="pack" and @value="0"]'))
        time.sleep(2)

    def package2(self):
        # 精品包装
        self.base_click((By.XPATH, '//input[@name="pack" and @value="1"]'))

    # 祝福贺卡
    def card1(self):
        # 不要贺卡
        self.base_click((By.XPATH, '//input[@name="card" and @value="0"]'))

    def card2(self):
        # 祝福贺卡
        self.base_click((By.XPATH, '//input[@name="card" and @value="1"]'))

    def greetings(self):
        # 祝福语
        self.base_send_keys((By.NAME, 'card_message'), "祝福")
        time.sleep(2)

    # 其他信息
    # 使用红包
    def red_envelope(self):
        # 输入红包序列号
        self.base_send_keys((By.NAME, 'bonus_sn'), "123")
        time.sleep(1)
        # 验证红包
        self.base_click((By.NAME, 'validate_bonus'))
        # 确认弹窗
        self.base_alert_operation()

    # 开发票
    def invoice1(self):
        # 勾选发票按钮
        self.base_click((By.ID, 'ECS_NEEDINV'))

    def invoice2(self):
        # 选择下拉框
        self.base_select_index((By.ID, 'ECS_INVTYPE'), index=1)

    def invoice3(self):
        # 发票抬头
        self.base_send_keys((By.ID, 'ECS_INVPAYEE'), '12345')

    # 订单附言
    def postscript(self):
        self.base_send_keys((By.ID, 'postscript'), '谢谢')

    # 缺货处理
    def stock1(self):
        # 取消订单
        self.base_click((By.XPATH, '//input[@name="how_oos" and @value="1"]'))

    def stock2(self):
        # 与店主协商
        self.base_click((By.XPATH, '//input[@name="how_oos" and @value="2"]'))

    def stock3(self):
        # 等待所有商备齐后再发
        self.base_click((By.XPATH, '//input[@name="how_oos" and @value="0"]'))

    # 提交订单
    def submit_order(self):
        self.base_click((By.XPATH, '//div[@class="flowBox"]/div[2]/input'))


if __name__ == '__main__':
    driver = open_browser()
    order = Order(driver)
    # 调用登录
    login = LoginPage(driver)
    login.base_get('http://139.129.26.163//user.php')
    login.input_username('xiao')
    login.input_password('123456')
    login.remember()
    login.login_btn()
    # 去首页
    index = IndexPage(driver)
    index.home_btn()
    # 调用购买商品
    shop_cart = ShopCartPage(driver)
    shop_cart.look_goods((By.XPATH, '//img[@alt="炫彩翻页保护套"]'))
    shop_cart.buy_goods()
    # 去结算
    shop_cart.clearance()
    # 生成订单页面
    order.distribution_method1()
    # order.distribution_method2()
    order.pay1()
    order.package1()
    order.card2()
    order.greetings()
    time.sleep(2)
    order.red_envelope()
    time.sleep(1)
    order.invoice1()
    order.invoice2()
    order.invoice3()
    time.sleep(1)
    order.postscript()
    order.stock2()
    time.sleep(5)
    order.submit_order()
    time.sleep(5)
    driver.quit()
